package com.pxst.agent.controller;

import com.pxst.agent.entity.SysAgents;
import com.pxst.agent.req.AgentCommissionReq;
import com.pxst.agent.req.AgentReq;
import com.pxst.agent.req.AgentCreatReq;
import com.pxst.agent.req.AgentUpdateReq;
import com.pxst.agent.resp.AgentByIdResp;
import com.pxst.agent.resp.AgentCommissionResp;
import com.pxst.agent.resp.AgentResp;
import com.pxst.agent.resp.AgentSearchResp;
import com.pxst.agent.service.SysAgentsService;
import com.pxst.annotations.LogOperate;
import com.pxst.resp.ApiResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
* @author zarke
* @since 2023-09-08 11:15:17
*/
@RestController
@RequestMapping("/api/agent")
@Api(tags = "代理相关接口")
public class SysAgentsController {

    @Autowired
    private SysAgentsService sysAgentsService;

    @GetMapping("/simple")
    @ApiOperation(value = "代理下拉框信息")
        public ApiResp<AgentResp> getAgentList(@RequestParam("with") String with , @RequestParam("status") Integer status) {
        if ("identity,belong_id".equals(with) && status == null){
            return sysAgentsService.getAgentList(status);
        }else if ("identity,belong_id".equals(with) && status == 1){
            return sysAgentsService.getAgentList(status);
        }else {
            return ApiResp.bussError("参数错误。");
        }
    }

    @GetMapping
    @ApiOperation(value = "搜索代理")
    public ApiResp<AgentSearchResp> getAgents(AgentReq agentReq) {
        return sysAgentsService.getAgents(agentReq);
    }

    @PostMapping
    @ApiOperation(value = "新增代理")
    @LogOperate(operate = "新增代理", service = SysAgentsService.class, type = "add")
    @PreAuthorize("@ss.hasAnyPermissions('agent:list:create')")
    public ApiResp<SysAgents> create(@RequestBody AgentCreatReq agentReq1) {
        return sysAgentsService.create(agentReq1);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "代理详情")
    public ApiResp<AgentByIdResp> getAgent(@PathVariable Long id) {
        SysAgents byId = sysAgentsService.getById(id);
        AgentByIdResp agent = new AgentByIdResp();
        BeanUtils.copyProperties(byId,agent);
        return ApiResp.sucess(agent);
    }

    @PutMapping("/{id}/password/reset")
    @ApiOperation(value = "重置密码")
    @LogOperate(operate = "重置密码", service = SysAgentsService.class, type = "edit")
    @PreAuthorize("@ss.hasAnyPermissions('agent:password:update')")
    public ApiResp<Map<String,String>> updatePassword(@PathVariable Long id) {
        return sysAgentsService.updatePassword(id);
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "编辑")
    @LogOperate(operate = "编辑", service = SysAgentsService.class, type = "edit")
    @PreAuthorize("@ss.hasAnyPermissions('agent:details:update')")
    public ApiResp<AgentUpdateReq> updateDetails(@RequestBody AgentUpdateReq updateReq) {
        return sysAgentsService.updateDetails(updateReq);
    }

    @GetMapping("/{id}/plans/summary")
    @ApiOperation(value = "搜索代理抽成列表")
    public ApiResp<AgentCommissionResp> getAgentCommission(@PathVariable Long id , AgentCommissionReq agentReq) {
        return sysAgentsService.getAgentCommission(id ,agentReq);
    }

}
